﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
namespace WPFExcelReport
{
    //
    public class JLTAV37Data : BaseFunction
    {
        public string KSTKRS = "";
        public string KST = "";
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        public INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        SETINTEXDB INTEXDB = new SETINTEXDB();
        public JLTAV37Data()
            : base()
        {

        }
        public DataSet LoadEnv()
        {
            DataSet SResult = new DataSet();
            DataSet SReturn = new DataSet();
            DataRow dwData;
            #region=======QueryAMSTAMM=======
            dwData = INTEXDB.AMSTAMM.NewRow();
            dwData["AMMANDANT"] = "00";
            dwData["AMKSTKRS"] = "  ";
            dwData["AMKST"] = "0210";
            SResult = INTEXDBC.QueryAMSTAMM(dwData, 1);
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                row["AMBEZ"] = Helpers.Helper.GetGB2312Value(row["AMBEZ"].ToString().Trim()); 
                row["AMAENDZL"] = 1;//排产最后日期
                row["AMAENDDAT"] = 0;//排产最后日期
                row["AMNEUDAT"] = "0000";//排产最后时间
            }
            SReturn.Merge(SResult);
            INTEXDBC.DeleteICC_STDMACHINE(null, 1);
            INTEXDBC.InsertICC_STDMACHINE(null, 1);
            //SResult = INTEXDBC.QueryICC_STDMACHINE(dwData, 1); 
            SResult = INTEXDBC.QueryAMSTAMM(dwData, 2);
            SResult.Tables[0].TableName = "ICC_STDMACHINE";
            SReturn.Merge(SResult);
            SResult = INTEXDBC.QueryICC_SRULES(null, 9999);
            SReturn.Merge(SResult);
            #endregion
            return SReturn;
        }
        public DataSet BuildData(DataRow dwInput)
        {
            DataSet Result = new DataSet();
            DataSet Return = new DataSet();
            DataRow dwABKOPF = null;
            string KRS = "";
            string LFD = "";
            try
            {
                DataRow dwData = null;
                #region=======ICC_MKRF=======
                dwData = INTEXDB.ICC_MKRF.NewRow();
                dwData["ABFMANDANT"] = dwInput["ABFMANDANT"];
                dwData["ABFKSTKRS"] = dwInput["ABFKSTKRS"];
                dwData["ABFKST"] = dwInput["ABFKST"];
                Result = INTEXDBC.QueryICC_MKRF(dwData, 2);
                Return.Merge(Result);
                #endregion
                #region=======QueryICC_PVKPOS=======
                dwData = INTEXDB.ICC_PVKPOS.NewRow();
                dwData["VKPMANDANT"] = dwInput["ABFMANDANT"];
                Result = INTEXDBC.QueryICC_PVKPOS(dwData, 1);
                Return.Merge(Result);
                #endregion
                #region=======QueryICC_JWYARN======= 
                dwData = INTEXDB.ICC_PVKPOS.NewRow();
                dwData["VKPMANDANT"] = dwInput["ABFMANDANT"];
                Result = INTEXDBC.QueryICC_JWYARN(dwData, 2);
                Return.Merge(Result);
                #endregion

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return Return;
        }
      
        private DataRow NewMKRF()
        {
            DataRow dwData = INTEXDB.MKRF.NewRow();
            foreach (DataColumn cm in INTEXDB.MKRF.Columns)
            {
                if (dwData[cm.ColumnName].ToString() == "")
                {
                    if (cm.DataType.ToString().ToLower().Contains("string"))
                    {
                        dwData[cm.ColumnName] = "".PadRight(cm.MaxLength);
                    }
                    else
                    {
                        dwData[cm.ColumnName] = 0;
                    }
                }
            }
            return dwData;
        }
        public void SaveTempSchedule(DataSet src)
        {
            DataRow dwData;
            //找到对应的记录，进行更新
            DataSet SResult;
            DataRow dwMKRFS = null;
            Helpers.Helper.GetDate(ref date, ref year, ref month, ref day);
            foreach (DataRow dwMKRF in src.Tables["ICC_MKRF"].Rows)
            {
                INTEXDBC.SaveICC_MKRF(dwMKRF);
                dwMKRFS = null;
                dwData = NewMKRF();
                dwData["MKFMANDANT"] = "00";
                dwData["MKFAUFKRS"] = dwMKRF["ABFAUFKRS"];
                dwData["MKFAUFLFD"] = dwMKRF["ABFAUFLFD"];
                dwData["MKFAUFPOS"] = dwMKRF["ABFAUFPOS"];
                dwData["MKFAFOLGE"] = dwMKRF["ABFAFOLGE"];
                SResult = INTEXDBC.QueryMKRF(dwData, 1);
                foreach(DataRow row in SResult.Tables[0].Rows)
                {
                    dwMKRFS = row;
                }
                if (dwMKRFS == null)
                {
                    dwMKRFS = dwData;
                    dwData["MKFTP"] = "0000";
                    dwData["MKFRWLFD"] = "000000";
                    dwData["MKFRWPOS"] = "0000";
                    dwData["MKFRGBWERT1"] = "000";
                    dwData["MKFRGBWERT2"] = "000";
                    dwData["MKFRGBWERT3"] = "000";
                    dwData["MKFFRBH"] = "000";
                    dwData["MKFMODNR"] = "000";
                    dwData["MKFEINSTNR"] = "000"; ;
                    dwData["MKFNBWCHA"] = "00"; ;
                    dwData["MKFMAUFLFD"] = "000000"; ;
                    dwData["MKFMAUFPOS"] = "000"; ;
                    dwData["MKFMAFOLGE"] = "000"; ;
                    dwData["MKFMTP"] = "0000";
                    dwData["MKFVMLFDNR"] = "0000"; ;
                    dwData["MKFVMPOS"] = "00";
                    dwData["MKFNEUB"] = app.User;
                    dwData["MKFNEUD"] = date;
                }
                if (dwMKRF["MKFERLKZ"].ToString()== "J")
                    continue;
                dwData["MKFMNRKRS"] = dwMKRF["ABFBNRKRS"];
                dwData["MKFMLFDNR"] = dwMKRF["ABFBLFDNR"];
                dwData["MKFMPOS"] = dwMKRF["ABFBPOS"];
                dwData["MKFLFDNR"] = dwMKRF["MKFLFDNR"];
           
                dwData["MKFERLKZ"] = dwMKRF["MKFERLKZ"];
                dwData["MKFERLDAT"] = dwMKRF["MKFERLDAT"];
                dwData["MKFAUFKRS"] = dwMKRF["ABFAUFKRS"];
                dwData["MKFAUFLFD"] = dwMKRF["ABFAUFLFD"];
                dwData["MKFAUFPOS"] = dwMKRF["ABFAUFPOS"];
                dwData["MKFAFOLGE"] = dwMKRF["ABFAFOLGE"];
                dwData["MKFBEARBZT"] = Convert.ToDouble(dwMKRF["ABFMASCHZT"].ToString()) / 100;
         
                dwData["MKFPLDATV"] = dwMKRF["MKFPLDATV"];
                dwData["MKFSCHICHTV"] = dwMKRF["MKFSCHICHTV"];
                dwData["MKFSME"] = dwMKRF["ABFSOLLME"];
                dwData["MKFSMG5"] = dwMKRF["ABFSOLLMG"];          
                INTEXDBC.SaveMKRF(dwData, app.company);
            }
        }
        public void SaveSchedule(DataRow dwMKRF, bool IsConfirm = false)
        {
            DataSet SResult;
            DataRow dwData;
            try
            {
                dwMKRF["SPCONFIRM"] = "YES     ";
                
                INTEXDBC.UpdateICC_MKRF(dwMKRF, 1);
                dwData = NewMKRF();
                dwData["MKFMANDANT"] = "00";
                dwData["MKFAUFKRS"] = dwMKRF["ABFAUFKRS"];
                dwData["MKFAUFLFD"] = dwMKRF["ABFAUFLFD"];
                dwData["MKFAUFPOS"] = dwMKRF["ABFAUFPOS"];
                dwData["MKFAFOLGE"] = dwMKRF["ABFAFOLGE"];
                dwData["MKFMNRKRS"] = dwMKRF["ABFBNRKRS"];
                dwData["MKFMLFDNR"] = dwMKRF["ABFBLFDNR"];
                dwData["MKFMPOS"] = dwMKRF["ABFBPOS"];
                dwData["MKFLFDNR"] = dwMKRF["MKFLFDNR"];

                dwData["MKFERLKZ"] = dwMKRF["MKFERLKZ"];
                dwData["MKFERLDAT"] = dwMKRF["MKFERLDAT"];
                dwData["MKFAUFKRS"] = dwMKRF["ABFAUFKRS"];
                dwData["MKFAUFLFD"] = dwMKRF["ABFAUFLFD"];
                dwData["MKFAUFPOS"] = dwMKRF["ABFAUFPOS"];
                dwData["MKFAFOLGE"] = dwMKRF["ABFAFOLGE"];

                dwData["MKFPLDATV"] = dwMKRF["MKFPLDATV"];
                dwData["MKFSCHICHTV"] = dwMKRF["MKFSCHICHTV"];
                dwData["MKFBEARBZT"] = Convert.ToDouble(dwMKRF["ABFMASCHZT"].ToString()) / 100;
                dwData["MKFSME"] = dwMKRF["ABFSOLLME"];
                dwData["MKFSMG5"] = dwMKRF["ABFSOLLMG"];          
                INTEXDBC.UpdateMKRF(dwData, 1);         
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}